﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;
public partial class DownloadNewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonDownload_Click(object sender, EventArgs e)
    {
        ShowNews("sina");
        return;

        string newsUrl = TextBoxUrl.Text;        
        byte[] htmlNews = new WebClient().DownloadData(newsUrl);
        string srcCode = Encoding.GetEncoding("GB2312").GetString(htmlNews);
        int start, end;
        string newsTitle, newsContent;
        string content = "";
        start = srcCode.IndexOf("artibodyTitle");
        end = srcCode.IndexOf("publish_helper_end");        
        content = srcCode.Substring(start, end - start);

        start = content.IndexOf('>');
        end = content.IndexOf('<');
        newsTitle = content.Substring(start+1, end - start -1);

        start = content.IndexOf("id=\"artibody\"");
        end = content.LastIndexOf("<!--");        
        newsContent = content.Substring(start + 14, end - start - 15);

        LabelTitle.Text = newsTitle;
        LabelContent.Text = newsContent;
    }
    protected void ButtonSohu_Click(object sender, EventArgs e)
    {
        string newsUrl = TextBoxUrl.Text;
        byte[] htmlNews = new WebClient().DownloadData(newsUrl);
        string srcCode = Encoding.GetEncoding("GB2312").GetString(htmlNews);
        int start, end;
        string newsTitle, newsContent;
        string mainS, mainE, titleS, titleE, bodyS, bodyE;
        int mainLen, titleLen, bodyLen;
        mainS = "id=\"contentB\"";
        mainE = "<!--内容区 end-->";
        titleS = "<h2>";
        titleE = "</h2>";
        bodyS = "id=\"contentText\">";
        bodyE = "<SOHUADCODE>";
        string content = "";
        start = srcCode.IndexOf(mainS);
        end = srcCode.IndexOf(mainE);
        content = srcCode.Substring(start, end - start);//求得主要内容文本

        start = content.IndexOf(titleS) + titleS.Length;
        end = content.IndexOf(titleE);
        newsTitle = content.Substring(start, end - start);//求得标题

        start = content.IndexOf(bodyS) + bodyS.Length;
        end = content.LastIndexOf(bodyE);
        //end = content.Length;
        
        newsContent = content.Substring(start, end - start);
        ///////////////////////////删除、替换掉广告等内容
        string[] adS={ "<table", "<SCRIPT>"}, adE={ "</table>", "</SCRIPT>"};
        int k = adS.Length;
        for(int n = 0; n < k; n++)
        {
            start = newsContent.IndexOf(adS[n]);
            end = newsContent.IndexOf(adE[n]);
            if(start > -1 && end > start)
            {
                newsContent = newsContent.Remove(start, end - start + adE[n].Length);
            }
        }
        ///////////////////////////
        LabelTitle.Text = newsTitle;
        LabelContent.Text = newsContent;
    }
    protected void Button163_Click(object sender, EventArgs e)
    {
        int start, end;
        string newsTitle, newsContent;
        string mainS = "", mainE = "", titleS = "", titleE = "", bodyS = "", bodyE = "", delStart = "", delEnd = "", pageEncode = "";
        int mainLen, titleLen, bodyLen;
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/SiteHtmlNewsTag.xml"));
        XmlElement root = doc.DocumentElement;
        XmlNodeList sites = root.ChildNodes;
        foreach(XmlNode s in sites)
        {
            if(s.Attributes["id"].Value == "163")//查找正确的站点节点
            {
                mainS = s.SelectSingleNode("mainStart").InnerText;
                mainE = s.SelectSingleNode("mainEnd").InnerText;
                titleS = s.SelectSingleNode("titleStart").InnerText;
                titleE = s.SelectSingleNode("titleEnd").InnerText;
                bodyS = s.SelectSingleNode("bodyStart").InnerText;
                bodyE = s.SelectSingleNode("bodyEnd").InnerText;                
                delStart = s.SelectSingleNode("deleteStartTags").InnerText;
                delEnd = s.SelectSingleNode("deleteEndTags").InnerText;
                pageEncode = s.Attributes["pageEncode"].Value;
                break;
            }
        }
        string newsUrl = TextBoxUrl.Text;
        byte[] htmlNews = new WebClient().DownloadData(newsUrl);
        string srcCode = Encoding.GetEncoding(pageEncode).GetString(htmlNews);
        
        
        string content = "";
        start = srcCode.IndexOf(mainS);
        end = srcCode.IndexOf(mainE);
        content = srcCode.Substring(start, end - start);//求得主要内容文本

        start = content.IndexOf(titleS) + titleS.Length;
        end = content.IndexOf(titleE);
        newsTitle = content.Substring(start, end - start);//求得标题

        start = content.IndexOf(bodyS) + bodyS.Length;
        end = content.LastIndexOf(bodyE);
        //end = content.Length;
        
        newsContent = content.Substring(start, end - start);
        ///////////////////////////删除、替换掉广告等内容
        if(delStart != "")
        {
            string[] adS = delStart.Split(','), adE = delEnd.Split(',');
            int k = adS.Length;
            for(int n = 0; n < k; n++)
            {
                start = newsContent.IndexOf(adS[n]);
                end = newsContent.IndexOf(adE[n]);
                if(start > -1 && end > start)
                {
                    newsContent = newsContent.Remove(start, end - start + adE[n].Length);
                }
            }
        }
        ///////////////////////////
        LabelTitle.Text = newsTitle;
        LabelContent.Text = newsContent;    
    }
    protected void Buttondf_Click(object sender, EventArgs e)
    {
        int start, end;
        string newsTitle, newsContent;
        string mainS = "", mainE = "", titleS = "", titleE = "", bodyS = "", bodyE = "", delStart = "", delEnd = "", pageEncode = "";
        int mainLen, titleLen, bodyLen;
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/SiteHtmlNewsTag.xml"));
        XmlElement root = doc.DocumentElement;
        XmlNodeList sites = root.ChildNodes;
        foreach(XmlNode s in sites)
        {
            if(s.Attributes["id"].Value == "df")//查找正确的站点节点
            {
                mainS = s.SelectSingleNode("mainStart").InnerText;
                mainE = s.SelectSingleNode("mainEnd").InnerText;
                titleS = s.SelectSingleNode("titleStart").InnerText;
                titleE = s.SelectSingleNode("titleEnd").InnerText;
                bodyS = s.SelectSingleNode("bodyStart").InnerText;
                bodyE = s.SelectSingleNode("bodyEnd").InnerText;
                delStart = s.SelectSingleNode("deleteStartTags").InnerText;
                delEnd = s.SelectSingleNode("deleteEndTags").InnerText;
                pageEncode = s.Attributes["pageEncode"].Value;
                break;
            }
        }
        string newsUrl = TextBoxUrl.Text;
        byte[] htmlNews = new WebClient().DownloadData(newsUrl);
        string srcCode = Encoding.GetEncoding(pageEncode).GetString(htmlNews);


        string content = "";
        start = srcCode.IndexOf(mainS);
        end = srcCode.IndexOf(mainE);
        if(start < 0) //没有匹配的开始标记，返回
        {
            return;
        }
        content = srcCode.Substring(start, end - start);//求得主要内容文本

        start = content.IndexOf(titleS) + titleS.Length;
        end = content.IndexOf(titleE);
        newsTitle = content.Substring(start, end - start);//求得标题

        start = content.IndexOf(bodyS) + bodyS.Length;
        end = content.LastIndexOf(bodyE);
        if(end < 0 || end < start)
        {
            end = content.Length;
        }
        newsContent = content.Substring(start, end - start);
        ///////////////////////////删除、替换掉广告等内容
        if(delStart != "")
        {
            string[] adS = delStart.Split(','), adE = delEnd.Split(',');
            int k = adS.Length;
            for(int n = 0; n < k; n++)
            {
                start = newsContent.IndexOf(adS[n]);
                end = newsContent.IndexOf(adE[n]);
                if(start > -1 && end > start)
                {
                    newsContent = newsContent.Remove(start, end - start + adE[n].Length);
                }
            }
        }
        ///////////////////////////
        LabelTitle.Text = newsTitle;
        LabelContent.Text = newsContent; 
    }
    protected void ButtonRed_Click(object sender, EventArgs e)
    {
        ShowNews("red");
    }
    protected void ButtonPeople_Click(object sender, EventArgs e)
    {
        ShowNews("people");
    }
    protected void ButtonXH_Click(object sender, EventArgs e)
    {
        ShowNews("xh");
    }
    protected void ButtonSC_Click(object sender, EventArgs e)
    {
        ShowNews("sc");
    }
    protected void ShowNews(string id)
    {
        int startT, endT, startC, endC, start, end;
        string newsTitle, newsContent;
        string mainS = "", mainE = "", titleS = "", titleE = "", bodyS = "", bodyE = "", delStart = "", delEnd = "", pageEncode = "";
        int mainLen, titleLen, bodyLen;
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/SiteHtmlNewsTag.xml"));
        XmlElement root = doc.DocumentElement;
        XmlNodeList sites = root.ChildNodes;
        foreach(XmlNode s in sites)
        {
            if(s.Attributes["id"].Value == id)//查找正确的站点节点
            {
                mainS = s.SelectSingleNode("mainStart").InnerText;
                mainE = s.SelectSingleNode("mainEnd").InnerText;
                titleS = s.SelectSingleNode("titleStart").InnerText;
                titleE = s.SelectSingleNode("titleEnd").InnerText;
                bodyS = s.SelectSingleNode("bodyStart").InnerText;
                bodyE = s.SelectSingleNode("bodyEnd").InnerText;
                delStart = s.SelectSingleNode("deleteStartTags").InnerText;
                delEnd = s.SelectSingleNode("deleteEndTags").InnerText;
                pageEncode = s.Attributes["pageEncode"].Value;
                break;
            }
        }
        string newsUrl = TextBoxUrl.Text;
        byte[] htmlNews = new WebClient().DownloadData(newsUrl);
        string srcCode = Encoding.GetEncoding(pageEncode).GetString(htmlNews);
        string content = "";
        //start = srcCode.IndexOf(mainS);
        //if(start < 0) //没有匹配的开始标记，返回
        //{
        //    return;
        //}
        //end = srcCode.IndexOf(mainE, start);
        //if(end < 0)
        //{
        //    return;
        //}
        //content = srcCode.Substring(start, end - start);//求得主要内容文本

        startT = srcCode.IndexOf(titleS) + titleS.Length;
        if(startT < 0)
        {
            return;
        }
        endT = srcCode.IndexOf(titleE, startT);
        if(endT < 0)
        {
            return;
        }
        if(titleS.IndexOf('>') > 0)
        {
            newsTitle = srcCode.Substring(startT, endT - startT);//求得标题
        }
        else
        {
            startT = srcCode.IndexOf('>', startT) + 1;
            newsTitle = srcCode.Substring(startT, endT - startT);
        }
        startC = srcCode.IndexOf(bodyS, startT) + bodyS.Length;
        if(startC < 0)
        {
            return;
        }
        endC = srcCode.IndexOf(mainE, startC);    //第一次主要结束    
        if(endC < 0)
        {
            return;
        }
        newsContent = srcCode.Substring(startC, endC - startC);//第一次结尾
        if(bodyE != "")
        {
            bodyLen = newsContent.LastIndexOf(bodyE);
            newsContent = newsContent.Substring(0, bodyLen);//第二次结尾
        }
        ///////////////////////////删除、替换掉广告等内容
        if(delStart != "")
        {
            string[] adS = delStart.Split(','), adE = delEnd.Split(',');
            int k = adS.Length;
            for(int n = 0; n < k; n++)
            {
                start = newsContent.IndexOf(adS[n]);
                end = newsContent.IndexOf(adE[n]);
                if(start > -1 && end > start)
                {
                    newsContent = newsContent.Remove(start, end - start + adE[n].Length);
                }
            }
        }
        ///////////////////////////
        LabelTitle.Text = newsTitle;
        LabelContent.Text = newsContent;
    }
    protected void ShowNewsN(string id)
    {
        int start, end;
        string newsTitle, newsContent;
        string mainS = "", mainE = "", titleS = "", titleE = "", bodyS = "", bodyE = "", delStart = "", delEnd = "", pageEncode = "";
        int mainLen, titleLen, bodyLen;
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/SiteHtmlNewsTag.xml"));
        XmlElement root = doc.DocumentElement;
        XmlNodeList sites = root.ChildNodes;
        foreach(XmlNode s in sites)
        {
            if(s.Attributes["id"].Value == id)//查找正确的站点节点
            {
                mainS = s.SelectSingleNode("mainStart").InnerText;
                mainE = s.SelectSingleNode("mainEnd").InnerText;
                titleS = s.SelectSingleNode("titleStart").InnerText;
                titleE = s.SelectSingleNode("titleEnd").InnerText;
                bodyS = s.SelectSingleNode("bodyStart").InnerText;
                bodyE = s.SelectSingleNode("bodyEnd").InnerText;
                delStart = s.SelectSingleNode("deleteStartTags").InnerText;
                delEnd = s.SelectSingleNode("deleteEndTags").InnerText;
                pageEncode = s.Attributes["pageEncode"].Value;
                break;
            }
        }
        string newsUrl = TextBoxUrl.Text;
        byte[] htmlNews = new WebClient().DownloadData(newsUrl);
        string srcCode = Encoding.GetEncoding(pageEncode).GetString(htmlNews);


        string content = "";
        start = srcCode.IndexOf(mainS);
        if(start < 0) //没有匹配的开始标记，返回
        {
            return;
        }
        end = srcCode.IndexOf(mainE, start);
        if(end < 0)
        {
            return;
        }
        content = srcCode.Substring(start, end - start);//求得主要内容文本

        start = content.IndexOf(titleS) + titleS.Length;
        end = content.IndexOf(titleE);
        newsTitle = content.Substring(start, end - start);//求得标题

        start = content.IndexOf(bodyS) + bodyS.Length;
        end = content.LastIndexOf(bodyE);
        if(end < 0 || end < start)
        {
            end = content.Length;
        }
        newsContent = content.Substring(start, end - start);
        ///////////////////////////删除、替换掉广告等内容
        if(delStart != "")
        {
            string[] adS = delStart.Split(','), adE = delEnd.Split(',');
            int k = adS.Length;
            for(int n = 0; n < k; n++)
            {
                start = newsContent.IndexOf(adS[n]);
                end = newsContent.IndexOf(adE[n]);
                if(start > -1 && end > start)
                {
                    newsContent = newsContent.Remove(start, end - start + adE[n].Length);
                }
            }
        }
        ///////////////////////////
        LabelTitle.Text = newsTitle;
        LabelContent.Text = newsContent;
    }

    protected void ShowNewsOld(string id)
    {
        int start, end;
        string newsTitle, newsContent;
        string mainS = "", mainE = "", titleS = "", titleE = "", bodyS = "", bodyE = "", delStart = "", delEnd = "", pageEncode = "";
        int mainLen, titleLen, bodyLen;
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/SiteHtmlNewsTag.xml"));
        XmlElement root = doc.DocumentElement;
        XmlNodeList sites = root.ChildNodes;
        foreach(XmlNode s in sites)
        {
            if(s.Attributes["id"].Value == id)//查找正确的站点节点
            {
                mainS = s.SelectSingleNode("mainStart").InnerText;
                mainE = s.SelectSingleNode("mainEnd").InnerText;
                titleS = s.SelectSingleNode("titleStart").InnerText;
                titleE = s.SelectSingleNode("titleEnd").InnerText;
                bodyS = s.SelectSingleNode("bodyStart").InnerText;
                bodyE = s.SelectSingleNode("bodyEnd").InnerText;
                delStart = s.SelectSingleNode("deleteStartTags").InnerText;
                delEnd = s.SelectSingleNode("deleteEndTags").InnerText;
                pageEncode = s.Attributes["pageEncode"].Value;
                break;
            }
        }
        string newsUrl = TextBoxUrl.Text;
        byte[] htmlNews = new WebClient().DownloadData(newsUrl);
        string srcCode = Encoding.GetEncoding(pageEncode).GetString(htmlNews);


        string content = "";
        start = srcCode.IndexOf(mainS);
        if(start < 0) //没有匹配的开始标记，返回
        {
            return;
        }
        end = srcCode.IndexOf(mainE, start);
        if(end < 0)
        {
            return;
        }
        content = srcCode.Substring(start, end - start);//求得主要内容文本

        start = content.IndexOf(titleS) + titleS.Length;
        end = content.IndexOf(titleE);
        newsTitle = content.Substring(start, end - start);//求得标题

        start = content.IndexOf(bodyS) + bodyS.Length;
        end = content.LastIndexOf(bodyE);
        if(end < 0 || end < start)
        {
            end = content.Length;
        }
        newsContent = content.Substring(start, end - start);
        ///////////////////////////删除、替换掉广告等内容
        if(delStart != "")
        {
            string[] adS = delStart.Split(','), adE = delEnd.Split(',');
            int k = adS.Length;
            for(int n = 0; n < k; n++)
            {
                start = newsContent.IndexOf(adS[n]);
                end = newsContent.IndexOf(adE[n]);
                if(start > -1 && end > start)
                {
                    newsContent = newsContent.Remove(start, end - start + adE[n].Length);
                }
            }
        }
        ///////////////////////////
        LabelTitle.Text = newsTitle;
        LabelContent.Text = newsContent;
    }
    protected void ButtonCode_Click(object sender, EventArgs e)
    {
        string sourceCode = TextBoxSource.Text;string mS,mE,tS,tE, tdS, tdE, bS, bE, bdS, bdE;
        mS = txtMainS.Text; mE = txtMainE.Text; tS = txtTitleS.Text; tE = txtTitleE.Text;
        bS = txtBodyS.Text; bE = txtBodyE.Text;
        bdS = txtDelS.Text; bdE = txtDelE.Text;
        bool mBool = CutPage(mS, mE, ref sourceCode);
        string title = GetTitle(tS, tE, sourceCode, "", "");

        CutBody(bS, bE, ref sourceCode, "<table", false, 1, bdS, bdE);
        ReplaceHrefImgInContent(txtSiteUrl.Text, ref sourceCode);
        LabelContent.Text = sourceCode;
        LabelTitle.Text = title;
    }
    protected void SiteHandle(string id)
    {
        string newsTitle, newsContent;
        string mainS = "", mainE = "", titleS = "", titleE = "",titleDelS="",titleDelE="",
            bodyS = "", bodyE = "", removeTag="", delStart = "", delEnd = "", pageEncode = "";
        int mainLen, titleLen, bodyLen, removeNum=1;
        bool begin = true;
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/SiteHtmlNewsTag.xml"));
        XmlElement root = doc.DocumentElement;
        XmlNodeList sites = root.ChildNodes;
        foreach (XmlNode s in sites)
        {
            if (s.Attributes["id"].Value == id)//查找正确的站点节点
            {
                mainS = s.SelectSingleNode("mainStart").InnerText;
                mainE = s.SelectSingleNode("mainEnd").InnerText;
                titleS = s.SelectSingleNode("titleStart").InnerText;
                titleE = s.SelectSingleNode("titleEnd").InnerText;
                titleDelS = s.SelectSingleNode("titleDelStart").InnerText;
                titleDelE = s.SelectSingleNode("titleDelEnd").InnerText;
                bodyS = s.SelectSingleNode("bodyStart").InnerText;
                bodyE = s.SelectSingleNode("bodyEnd").InnerText;
                removeTag = s.SelectSingleNode("removeTag").InnerText;
                removeNum = int.Parse(s.SelectSingleNode("removeNum").InnerText);
                begin = s.SelectSingleNode("begin").InnerText == "true" ? true : false;
                delStart = s.SelectSingleNode("deleteStartTags").InnerText;
                delEnd = s.SelectSingleNode("deleteEndTags").InnerText;
                pageEncode = s.Attributes["pageEncode"].Value;
                break;
            }
        }
        string newsUrl = TextBoxUrl.Text;
        string siteUrl = newsUrl.Substring(0, newsUrl.LastIndexOf('/') + 1);
        byte[] htmlNews = new WebClient().DownloadData(newsUrl);
        string sourceCode = Encoding.GetEncoding(pageEncode).GetString(htmlNews);
       
        bool mBool = CutPage(mainS, mainE, ref sourceCode);
        if (!mBool)
        {
            LabelContent.Text = "没有匹配的内容。"; return;
        }
        string title = GetTitle(titleS, titleE, sourceCode, titleDelS, titleDelE);
        CutBody(bodyS, bodyE, ref sourceCode, removeTag, begin, removeNum, delStart, delEnd);
        ReplaceHrefImgInContent(siteUrl, ref sourceCode);
        LabelContent.Text = sourceCode;
        LabelTitle.Text = title;
    }
    /// <summary>
    /// 截取主要内容
    /// </summary>
    /// <param name="startTag">开始标记</param>
    /// <param name="endTag">结束标记</param>
    /// <param name="content">传入源文件，输出截取后的内容</param>
    /// <returns>是否成功</returns>
    protected bool CutPage(string startTag, string endTag, ref string content)
    {
        int start, end;
        start = content.IndexOf(startTag) + startTag.Length;
        end = content.IndexOf(endTag);
        if(start < 0 || end < 0)
        {
            return false;
        }
        content = content.Substring(start, end - start);//求得标题
        return true;
    }
    /// <summary>
    /// 截取文章内容
    /// </summary>
    /// <param name="startTag">文章开始标签，没有为空</param>
    /// <param name="endTag">文章结束大标记，没有为空</param>
    /// <param name="content">文章内容</param>
    /// <param name="removeEnd">文章结束精确标记,没有为空</param>
    /// <param name="begin">正数还是倒数搜索，true正数</param>
    /// <param name="nTag">搜索第几个精确标记,从1开始</param>
    /// <param name="delStartTag">删除的开始标记串,没有为空</param>
    /// <param name="delEndTag">删除的结束标记串，与开始配对</param>
    /// <returns></returns>
    protected bool CutBody(string startTag, string endTag, ref string content, string removeEnd, bool begin, int nTag, string delStartTag, string delEndTag)
    {
        int start, end;
        start = startTag == "" ? 0 : content.IndexOf(startTag) + startTag.Length;
        end = endTag == "" ? content.Length : content.IndexOf(endTag);
        if(start < 0 || end < 0)
        {
            return false;
        }
        content = content.Substring(start, end - start);//求得内容
        if(removeEnd != "")
        {
            start = 0;//设置开始搜索位置
            int temp = 0;
            if(begin)
            {
                for(int i = 0; i < nTag; i++)
                {
                    temp = content.IndexOf(removeEnd, start);
                    if(temp == -1)
                    {
                        break;
                    }
                    else
                    {
                        start = temp;
                    }
                }                
            }
            else
            {
                end = start = content.Length - 1;
                for(int i = 0; i < nTag; i++)
                {
                    temp = content.LastIndexOf(removeEnd, start);
                    if(temp == -1)
                    {
                        break;
                    }
                    else
                    {
                        start = temp;
                    }
                }
                //if (end == start) { start = 0; }
            }
            if(start > 0)
            {
                content = content.Remove(start);
            }
        }
        if(delStartTag != "")//删除文章中的标记
        {
            string[] delS, delE;
            delS = delStartTag.Split(',');
            delE = delEndTag.Split(',');
            int dn = delS.Length;
            for (int k = 0; k < dn; k++)
            {
                bool tagHave = true;
                while (tagHave)//搜索其中一个标记的所有匹配文本
                {
                    start = content.IndexOf(delS[k]);
                    if (start > -1)
                    {
                        end = content.IndexOf(delE[k], start);
                        if (end > -1)
                        {
                            content = content.Remove(start, end - start + delE[k].Length);//删除匹配标记的内容
                        }
                        else
                        {
                            tagHave = false;
                        }
                    }
                    else
                    {
                        tagHave = false;
                    }
                }
            }
        }
        return true;
    }
    /// <summary>
    /// 替换网站链接、图片为绝对路径
    /// </summary>
    /// <param name="siteUrl">当前网页的路径，到最后一个/处，如http://toook.com/news/fiance/  一定要加最后的/</param>
    /// <param name="content"></param>
    protected void ReplaceHrefImgInContent(string siteUrl, ref string content)
    {
        int start, end, posHref=0, posImg=0;
        //if (type == "href")
        //{
            while (true)
            {
                start = content.IndexOf("href=", posHref);
                if (start > -1)
                {
                    posHref = start + 5;
                    start = posHref;
                    end = content.IndexOf('.', start);
                    if (end > -1)
                    {
                        if (end - start > 100)//如果.距离href太远，说明不是自己的地址
                        {
                            break;
                        }
                        int temp = content.IndexOf("http:", start, 10);//http离href太远什么不是同一个地址
                        if (temp > -1)
                        {
                            posHref = start = temp + 5;
                            continue;
                        }
                        else
                        {
                            string tStr = content.Substring(start, end - start);
                            int t = tStr.Length;
                            for (int k = 0; k < t; k++)
                            {
                                if (tStr.Substring(k, 1) == "/")
                                {
                                    content = content.Insert(start, siteUrl.Substring(0, siteUrl.IndexOf('/', 10)));
                                    break;
                                }
                                else if (tStr.Substring(k, 1) == "\"")
                                {
                                    if (tStr.Substring(k + 1, 1) == "/")
                                    {
                                        content = content.Insert(start + 1 + k, siteUrl.Substring(0, siteUrl.IndexOf('/', 10)));
                                        break;
                                    }
                                    else
                                    {
                                        content = content.Insert(start + 1 + k, siteUrl); break;
                                    }
                                }
                                else if (tStr.Substring(k, 1) == "'")
                                {
                                    if (tStr.Substring(k + 1, 1) == "/")//网站根目录
                                    {
                                        content = content.Insert(start + 1 + k, siteUrl.Substring(0, siteUrl.IndexOf('/', 10)));
                                        break;
                                    }
                                    else//没有/ 表示在当前目录下
                                    {
                                        content = content.Insert(start + 1 + k, siteUrl); break;
                                    }
                                }
                                else if (tStr.Substring(k, 1) == " ")
                                {
                                    continue;
                                }
                                else//不是空格，" or ' or /
                                {
                                    content = content.Insert(start, siteUrl);
                                    break;
                                }
                            }
                        }
                    }
                    else//没有结束标记，跳出循环
                    {
                        break;
                    }
                }
                else//没有开始标记，跳出循环
                {
                    break;
                }
            }
        //处理图片路径
            while (true)
            {
                start = content.IndexOf("src=", posImg);
                if (start > -1)
                {
                    posImg = start + 4;
                    start = posImg;
                    end = content.IndexOf('>', start);
                    if (end > -1)
                    {
                        if (end - start > 100)//如果.距离href太远，说明不是自己的地址
                        {
                            break;
                        }
                        int temp = content.IndexOf("http:", start, 10);
                        if (temp > -1)
                        {
                            posImg = start = temp + 5;
                            continue;
                        }
                        else
                        {
                            string tStr = content.Substring(start, end - start);//取得图片地址部分
                            int t = tStr.Length;
                            for (int k = 0; k < t; k++)
                            {
                                if (tStr.Substring(k, 1) == "/")
                                {
                                    content = content.Insert(start, siteUrl.Substring(0, siteUrl.IndexOf('/', 10)));
                                    break;
                                }
                                else if (tStr.Substring(k, 1) == ".")
                                {
                                    int pp = tStr.LastIndexOf("../");
                                    if (pp < 0)//路径不对
                                    {
                                        break;
                                    }
                                    else if (pp == 0)//没有引号
                                    {
                                        content = content.Remove(start, 3);
                                        //content.Insert(start, siteUrl.Substring(0, siteUrl.LastIndexOf('/') + 1));
                                        content = content.Insert(start, siteUrl.Substring(0, siteUrl.LastIndexOf('/', siteUrl.Length - 2) + 1));
                                        break;
                                    }else if(pp == 1)//有引号
                                    {
                                        content = content.Remove(start + 1, 3);
                                        ////content.Insert(start + 1, siteUrl.Substring(0, siteUrl.LastIndexOf('/') +1));
                                        content = content.Insert(start + 1, siteUrl.Substring(0, siteUrl.LastIndexOf('/', siteUrl.Length - 2) + 1));
                                        break;
                                    }
                                    else
                                    {
                                        int rrr = pp / 3 + 1;

                                        string tUrl = siteUrl;
                                        for (int i = 0; i < rrr; i++)
                                        {
                                            tUrl = tUrl.Substring(0, tUrl.LastIndexOf('/', tUrl.Length - 2) + 1);
                                        }
                                        if (pp % 3 != 0)
                                        {
                                            content = content.Remove(start + 1, pp + 2);
                                            //content.Insert(start + 1, siteUrl.Substring(0, siteUrl.LastIndexOf('/') + 1));
                                            content = content.Insert(start + 1, tUrl);
                                        }
                                        else
                                        {
                                            content = content.Remove(start, pp + 3);
                                            //content.Insert(start, siteUrl.Substring(0, siteUrl.LastIndexOf('/') + 1));
                                            content = content.Insert(start, tUrl);
                                        }
                                        break;
                                    }
                                }
                                else if (tStr.Substring(k, 1) == "\"")
                                {
                                    continue;
                                    //if (tStr.Substring(k + 1, 1) == "/")
                                    //{
                                    //    content = content.Insert(start + 1 + k, siteUrl.Substring(0, siteUrl.IndexOf('/', 10)));
                                    //    break;
                                    //}
                                    //else
                                    //{
                                    //    content = content.Insert(start + 1 + k, siteUrl);
                                    //    break;
                                    //}
                                }
                                else if (tStr.Substring(k, 1) == "'")
                                {
                                    continue;
                                    //if (tStr.Substring(k + 1, 1) == "/")//网站根目录
                                    //{
                                    //    content = content.Insert(start + 1 + k, siteUrl.Substring(0, siteUrl.IndexOf('/', 10)));
                                    //    break;
                                    //}
                                    //else//没有/ 表示在当前目录下
                                    //{
                                    //    content = content.Insert(start + 1 + k, siteUrl); break;
                                    //}
                                }
                                else if (tStr.Substring(k, 1) == " ")
                                {
                                    continue;
                                }
                                else//不是空格，" or ' or /
                                {
                                    content = content.Insert(start, siteUrl); break;
                                }
                            }
                        }
                    }
                    else//没有结束标记，跳出循环
                    {
                        break;
                    }
                }
                else//没有开始标记，跳出循环
                {
                    break;
                }
            }
        //}
        //else if(type = "img")
        //{

        //}
    }
    /// <summary>
    /// 取得文章标题
    /// </summary>
    /// <param name="startTag">标题开始标签，没有为空串</param>
    /// <param name="endTag">标题结束标签，没有为空串</param>
    /// <param name="content">传入源代码</param>
    /// <param name="startDel">删除开始标记，没有为空串</param>
    /// <param name="endDel">删除结束标记，没有为空串</param>
    /// <returns>返回文章标题</returns>
    protected string GetTitle(string startTag, string endTag, string content, string startDel, string endDel)
    {
        int start, end;
        start = startTag == "" ? 0 : content.IndexOf(startTag) + startTag.Length;
        end = endTag == "" ? 0 : content.IndexOf(endTag);
        if(start < 0 || end < 0)
        {
            return "";
        }
        content = content.Substring(start, end - start);//求得标题
        if(startDel != "")
        {
            content = content.Substring(content.LastIndexOf(startDel) + startDel.Length);
        }
        if(endDel != "")
        {
            content = content.Substring(0, content.IndexOf(endDel));
        }
        return content;
    }
    protected void ButtonNewSina_Click(object sender, EventArgs e)
    {
        Button site = ((Button)sender);
        switch (site.ID)
        {
            case "ButtonNewSina": SiteHandle("sina"); break;
            case "ButtonNewSohu": SiteHandle("sohuA"); break;
            case "ButtonNewSohuB": SiteHandle("sohuB"); break;
            case "ButtonNewPeople": SiteHandle("people"); break;
            case "ButtonNewSC": SiteHandle("sc"); break;
            case "ButtonNewXH": SiteHandle("sina"); break;
            case "ButtonNew163": SiteHandle("163"); break;
            case "ButtonNewDB": SiteHandle("db"); break;
            case "ButtonNewDayoo": SiteHandle("dayoo"); break;
            case "ButtonNewDF": SiteHandle("df"); break;
            case "ButtonNewRed": SiteHandle("red"); break;
        }
        //SiteHandle("sina");
    }
    protected void ButtonCreatePage_Click(object sender, EventArgs e)
    {
        CreateMyNewsHtml myNews = new CreateMyNewsHtml();
        myNews.SiteNewsDetailHandle(tSid.Text, tNewsId.Text, "Test Title", tDay.Text, tUrl.Text, tKind.Text, tTempId.Text);
    }
}
